<template>
	<view>
		<u-popup :show="show" @close="show=false" bgColor="transparent" >
			<view class="box">
				<view class="f ai-c jc-s-b top">
					<view class="cancel" @click="show=false">取消</view>
					<view class="title">{{favoritesTitle}}</view>
					<view class="complete" @click="handleComplete">完成</view>
				</view>
				<view class="f ai-c input-box">
					<image src="../../static/list-favorites.png"></image>
					<view class="input f f1"> <u--input border="none" v-model="title"></u--input></view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {createFavorites,updateFavorites} from '@/api/mine.js'
	export default {
		name: "CreateFavorites",
		data() {
			return {
				show: false,
				title:"",
				favoritesTitle:'',
				type:null,
				favorites_id:null
			};
		},
		methods:{
			async handleComplete(){
				if(!this.title){
					uni.showToast({
						title:'请输入收藏夹名字',
						icon:'none'
					})
					return
				}
				if(this.type=='add'){
					await createFavorites({title:this.title})
					uni.showToast({
						title:'创建成功',
						icon:'none'
					})
				}else{
					await updateFavorites({title:this.title,favorites_id:this.favorites_id})
					uni.showToast({
						title:'修改成功',
						icon:'none'
					})
				}
				
				this.show=false
				this.$emit('changeCreateFavorites');
			},
			setPopup(type,data){
				console.log(type,'新建收藏夹')
				this.type=type
				if(type=='add'){
					this.favoritesTitle='新建收藏夹'
				}else{
					this.favorites_id=data.id
					this.title=data.title
					this.favoritesTitle='修改收藏夹'
				}
				this.show=true
			}
		}
	}
</script>

<style lang="scss" scoped>
	.box {
		background: #ffffff;
		border-radius: 24rpx 24rpx 0 0;
		padding: 32rpx;
		box-sizing: border-box;
		height: 500rpx;
		
		.input-box{
			margin-top: 78rpx;
			
			image{
				width: 40rpx;
				height: 40rpx;
				margin-right: 16rpx;
			}
			.input{
				border-bottom: 1px solid #e5e5e5;
			}
		}

		.top {
			font-size: 28rpx;

			.cancel {
				color: #878b8e;
			}

			.title {
				font-size: 30rpx;
				font-weight: 800;
			}

			.complete {
				color: #5369e3;
			}
		}
	}
</style>